package com.bsoft.gol.hcb.dao;

import com.bsoft.gol.hcb.utils.CommonUtils;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import hcn.base.MedicaltoolsCollectRecord;
import org.atag.dao.util.BaseDaoImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:医学工具、公式收藏记录Dao
 * @author: zhengcb10708
 * @create: 2021-07-29 11:17
 **/
public abstract class MedicaltoolsCollectRecordDao extends BaseDaoImpl<MedicaltoolsCollectRecord> {
    public MedicaltoolsCollectRecordDao() {
        this.setKeyField("id");
    }

    @DAOMethod(sql = "select count(1) from MedicaltoolsCollectRecord where calculatorCode=:calculatorCode and userId=:userId ")
    public abstract long getCountByCalculatorCodeAndUserId(@DAOParam("calculatorCode") String calculatorCode, @DAOParam("userId") String userId);

    @DAOMethod(sql = "from MedicaltoolsCollectRecord where calculatorCode=:calculatorCode and userId=:userId ")
    public abstract MedicaltoolsCollectRecord getMedicaltoolsCollectRecordByUserId(@DAOParam("calculatorCode") String calculatorCode, @DAOParam("userId") String userId);

    @DAOMethod(sql = " delete from MedicaltoolsCollectRecord where calculatorCode=:calculatorCode and userId=:userId  ")
    public abstract void deleteMedicaltoolsCollectRecordByUserId(@DAOParam("calculatorCode") String calculatorCode,@DAOParam("userId") String userId );


    public Map<String, Object> queryMedicaltoolsCollectRecordByUserId(final String calculatorCode,final String userId) {
        Map<String, Object> params = new HashMap<>();
        StringBuilder hsql = new StringBuilder();
        hsql.append("select * from medicalTools_collect_record where 1=1 ");
        if (CommonUtils.isNotEmpty(userId)) {
            hsql.append(" and user_id=:userId");
            params.put("userId", userId);
        }
        if (CommonUtils.isNotEmpty(calculatorCode)) {
            hsql.append(" and calculator_code=:calculatorCode");
            params.put("calculatorCode", calculatorCode);
        }
        List<Map<String, Object>> list = super.findListBySql(hsql.toString(), params, Map.class);
        return list.isEmpty() ? new HashMap<>() : list.get(0);
    }

    public List<Map<String,Object>> queryMedicalToolsCollectList(final String userId, final String calculatorCode,final String calculatorName, final int pageNo, final int pageSize) {
        Map<String, Object> params = new HashMap<>();
        StringBuilder hsql = new StringBuilder();
        hsql.append("select * from medicalTools_collect_record where 1=1 ");
        if (CommonUtils.isNotEmpty(userId)) {
            hsql.append(" and user_id=:userId");
            params.put("userId", userId);
        }
        if (CommonUtils.isNotEmpty(calculatorCode)) {
            hsql.append(" and calculator_code=:calculatorCode");
            params.put("calculatorCode", calculatorCode);
        }
        if (CommonUtils.isNotEmpty(calculatorName)) {
            hsql.append(" and calculator_name like :calculatorName");
            params.put("calculatorName", "%" + calculatorName + "%");

        }
        hsql.append(" order by create_date desc ");
        return super.findListBySql(hsql.toString(), params, Map.class, pageNo, pageSize);
    }
}
